package net.wds.ssbus.modules.dbconfig.service.impl;

import net.wds.ssbus.common.utils.PageUtils;
import net.wds.ssbus.common.utils.Query;
import net.wds.ssbus.modules.dbconfig.dao.TbDbSourceConfigPoolDao;
import net.wds.ssbus.modules.dbconfig.entity.DbSourceSelectEntity;
import net.wds.ssbus.modules.dbconfig.entity.TbDbSourceConfigEntity;
import net.wds.ssbus.modules.dbconfig.entity.TbDbSourceConfigPoolEntity;
import net.wds.ssbus.modules.dbconfig.service.DbSourceSelectService;
import net.wds.ssbus.modules.dbconfig.service.TbDbSourceConfigPoolService;
import net.wds.ssbus.modules.dbconfig.service.TbDbSourceConfigService;
import net.wds.ssbus.modules.dbconfig.service.TbDbSourceService;
import net.wds.ssbus.modules.dbconfig.vo.DbSourceConfigPoolVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;



@Service("tbDbSourceConfigPoolService")
public class TbDbSourceConfigPoolServiceImpl extends ServiceImpl<TbDbSourceConfigPoolDao, TbDbSourceConfigPoolEntity> implements TbDbSourceConfigPoolService {

    @Autowired
    TbDbSourceConfigService dbSourceConfigService;
    @Autowired
    TbDbSourceService dbSourceService;
    @Autowired
    DbSourceSelectService dbSourceSelectService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<TbDbSourceConfigPoolEntity> page = this.page(
                new Query<TbDbSourceConfigPoolEntity>().getPage(params),
                new QueryWrapper<TbDbSourceConfigPoolEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<DbSourceConfigPoolVo> getList(Integer dbId, Integer dbTypeId) {
        System.out.println(dbId+"------------"+dbTypeId);
        List<TbDbSourceConfigPoolEntity> poolEntities = this.list(new QueryWrapper<TbDbSourceConfigPoolEntity>().eq("db_type_id", dbTypeId));
//        List<TbDbSourceConfigEntity> sourceConfigEntity = dbSourceConfigService.list(new QueryWrapper<TbDbSourceConfigEntity>().eq("db_source_id", dbId));
        if (dbId == 0){
            List<DbSourceConfigPoolVo> collect = poolEntities.stream().map(item -> {
                DbSourceConfigPoolVo dbSourceConfigPoolVo = new DbSourceConfigPoolVo();
                BeanUtils.copyProperties(item, dbSourceConfigPoolVo);
                List<DbSourceSelectEntity> selectEntityList = dbSourceSelectService.list(new QueryWrapper<DbSourceSelectEntity>()
                        .eq("db_config_pool_id", item.getId())
                        .eq("db_source_id", dbTypeId));
                dbSourceConfigPoolVo.setSelectEntities(selectEntityList);
                return dbSourceConfigPoolVo;
            }).collect(Collectors.toList());
            return collect;
        }else {
            List<DbSourceConfigPoolVo> collect = poolEntities.stream().map(item -> {
                DbSourceConfigPoolVo dbSourceConfigPoolVo = new DbSourceConfigPoolVo();
                BeanUtils.copyProperties(item, dbSourceConfigPoolVo);
                TbDbSourceConfigEntity sourceConfigEntity = dbSourceConfigService.getConfValue(dbId, item.getEnName());
                if (sourceConfigEntity != null){
                    dbSourceConfigPoolVo.setConfType(sourceConfigEntity.getConfType());
                    dbSourceConfigPoolVo.setDbSourceId(sourceConfigEntity.getDbSourceId());
                    dbSourceConfigPoolVo.setConfValue(sourceConfigEntity.getConfValue());
                }
                List<DbSourceSelectEntity> selectEntityList = dbSourceSelectService.list(new QueryWrapper<DbSourceSelectEntity>()
                        .eq("db_config_pool_id", item.getId())
                        .eq("db_source_id", dbTypeId));
                dbSourceConfigPoolVo.setSelectEntities(selectEntityList);
                return dbSourceConfigPoolVo;
            }).collect(Collectors.toList());
            return collect;
        }
    }

}